package team.you.change.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.you.change.dao.CourseMapper;
import team.you.change.pojo.Course;
import team.you.change.util.Result;
import team.you.change.service.CourseService;
import team.you.change.util.YuoUtil;

import java.util.List;
import java.util.UUID;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public Result getAllCourses(Integer page, Integer limit) {
        Result result = new Result();
        List<Course> courses = courseMapper.selectAll(YuoUtil.getPage(page, limit), limit);
        Integer count = courseMapper.getCount();
        return YuoUtil.setData(result, courses, count);
    }

    @Override
    public Result del(String id) {
        Result result = new Result();
        Course course = courseMapper.selectByPrimaryKey(id);
        if (course != null) {
            try {
                return YuoUtil.crudMsg(courseMapper.deleteByPrimaryKey(id), result);
            }catch (Exception e){
               return YuoUtil.setErrorMsg(e, result);
            }
        } else {
            result.setCode(500);
            result.setMsg("该行不存在");
        }
        return result;
    }

    @Override
    @Transactional
    public Result delete(String[] ids) {
        Result result = new Result();
        try {
            for (int i = 0; i < ids.length; i++) {
                courseMapper.deleteByPrimaryKey(ids[i]);
            }
            return result;
        } catch (Exception e) {
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result update(Course course) {
        Result result = new Result();
        try {
            return YuoUtil.crudMsg(courseMapper.updateByPrimaryKey(course), result);
        } catch (Exception e) {
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result add(Course course) {
        Result result = new Result();
        String uuid = UUID.randomUUID().toString();
        course.setId(uuid);
        try {
            Result selCode = selCode(course.getCourseCode());
            if(selCode.getCode() == 0){
                return YuoUtil.crudMsg(courseMapper.insert(course), result);
            }
            return selCode;
        }catch (Exception e){
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result selectByLike(String value, Integer type, Integer page, Integer limit) {
        Result result = new Result();
        List<Course> courses = courseMapper.selectByLike(value, type, YuoUtil.getPage(page, limit), limit); //模糊查询时分页
        List<Course> count = courseMapper.selectByLike(value, type, null, null);
        return YuoUtil.setData(result, courses, count.size());
    }

    @Override
    public Result selCode(String code) {
        Result result = new Result();
        Course course = courseMapper.selectCode(code);
        if(course == null){
            return result;
        }else {
            result.setCode(500);
            result.setMsg("课程代码重复");
            return result;
        }
    }

    @Override
    public Result getCourseOption() {
        Result result = new Result();
        List<Course> courses = courseMapper.selectAllId();
        result.setData(courses);
        return result;
    }
}
